//函数调用可以提升
document.write(`${square(5)}<br>`);


/**
 * 声明定义函数一般形式
 * @param {Number} number 
 * @returns {Number}
 */
function square (number) {
    return number * number;
}
document.write(`${square(2)}<br>`);

//函数表达式不可以提升
// document.write(`${expr_square(5)}<br>`);

/**
 * 函数表达式通常使用 const 关键字定义,函数可以使用匿名函数
 * @param {Number} number 
 * @returns {Number}
 */
const expr_square = function (number) {
    return number * number;
}
document.write(`${expr_square(3)}<br>`);

document.write(`${count.name}<br>`);

//函数可以有自己的属性和方法count.call()
count.counter = 0;
function count() {
    return count.counter++;
}
document.write(`${count()}<br>`);
document.write(`${count()}<br>`);
document.write(`${count()}<br>`);

//函数嵌套
function fac(n) {
    return n < 2 ? 1 : n * fac(n - 1);
}
document.write(`${fac(1)}<br>`);
document.write(`${fac(2)}<br>`);
document.write(`${fac(3)}<br>`);
document.write(`${fac(4)}<br>`);
document.write(`${fac(5)}<br>`);
document.write(`${fac(6)}<br>`);

function fac2(n) {
    if (!(n in fac2)) {
        if (n < 2) {
            fac2[n] = 1;
        }
        fac2[n] = n * fac2(n - 1);
    } 
    return fac2[n];
}
fac2[1] = 1;
document.write(`${fac2(6)}<br>`);
document.write(`${fac2[5]}<br>`);
document.write(`${fac2[4]}<br>`);